package IC.Symbols;

import IC.SemanticError;
import IC.AST.Field;
import IC.AST.LibraryMethod;
import IC.AST.StaticMethod;
import IC.AST.VirtualMethod;
import IC.Types.Type;
import IC.Types.TypeTable;

public class ClassSymbolTable extends SymbolTable {

	public ClassSymbolTable(String id, SymbolTable parent) {
		super(id, parent);
	}
	
	public void insert(Field field) throws SemanticError{
		String name = field.getName();
		Type type = TypeTable.getType(field);
		FieldSymbol symbol = new FieldSymbol(name, Kind.FIELD, type, field.getOrder());
		Symbol previous = entries.put(name, symbol);
		if (previous != null){
			throw new SemanticError(field.getLine(), "a field/method with name " + name + " is already defined");
		}
	}
	
	public void insert(StaticMethod method) throws SemanticError{
		String name = method.getName();
		Type type = TypeTable.getType(method);
		MethodSymbol symbol = new MethodSymbol(name, Kind.STATIC_METHOD, type, true);
		Symbol previous = entries.put(name, symbol);
		if (previous != null){
			throw new SemanticError(method.getLine(), "a field/method with name " + name + " is already defined");
		}
	}
	
	public void insert(VirtualMethod method) throws SemanticError{
		String name = method.getName();
		Type type = TypeTable.getType(method);
		MethodSymbol symbol = new MethodSymbol(name, Kind.VIRTUAL_METHOD, type, false);
		Symbol previous = entries.put(name, symbol);
		if (previous != null){
			throw new SemanticError(method.getLine(), "a field/method with name " + name + " is already defined");
		}
	}
	
	public void insert(LibraryMethod method) throws SemanticError {
		String name = method.getName();
		Type type = TypeTable.getType(method);
		MethodSymbol symbol = new MethodSymbol(name, Kind.STATIC_METHOD, type, true);
		Symbol previous = entries.put(name, symbol);
		if (previous != null){
			throw new SemanticError(method.getLine(), "a field/method with name " + name + " is already defined");
		}		
	}

	public String getDisplayString(){
		return "Class Symbol Table: " + name;
	}
}
